package mapper

import (
	"github.com/beego/beego/v2/client/orm"
	context2 "golang.org/x/net/context"
	"ksd-social-api/modules/resume/model"
)

type ResumeTemplateCodeMapper struct{}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 查询简历模板和code是否存在
 */
func (mapper *ResumeTemplateCodeMapper) CountResumeTemplateCode(code string, opid uint64, systemId uint64) int {
	db := orm.NewOrm()
	var total int
	err := db.Raw("select count(1) from xk_resume_template_code where opid = ? and code = ? and mark = 0 and system_id = ?", opid, code, systemId).QueryRow(&total)
	if nil != err {
		return 0
	}
	return total
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据code和简历模板id删除兑换码
 */
func (mapper *ResumeTemplateCodeMapper) DelResumeTemplateCode(code string, opid uint64, systemId uint64) bool {
	db := orm.NewOrm()
	_, err := db.Raw("delete from xk_resume_template_code where opid = ? and code = ? and mark = 0 and system_id = ?", opid, code, systemId).Exec()
	if nil != err {
		return false
	}
	return true
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 标识兑换码已被使用
 */
func (mapper *ResumeTemplateCodeMapper) UpdateResumeTemplateCode(code string, opid uint64, systemId uint64) bool {
	db := orm.NewOrm()
	_, err := db.Raw("update xk_resume_template_code set opid = ? and mark = 1 where  code = ? and system_id = ?", opid, code, systemId).Exec()
	if nil != err {
		return false
	}
	return true
}

/**
 * 查询用户是否购买简历模板
 * @author feige
 * @date 2023-11-23
 * @version 1.0
 * @desc
 */
func (service *ResumeTemplateCodeMapper) CountUserBuyResumeTemplateNo(userId uint64, templateId uint64, systemId uint64) int {
	// 创建orm对象
	mysql := orm.NewOrm()
	var total int
	err := mysql.Raw("SELECT count(1) FROM xk_resume_template_user_buy WHERE user_id  = ? AND opid = ? and system_id = ?", userId, templateId, systemId).QueryRow(&total)
	// 返回
	if nil != err {
		return 0
	}
	return total
}

/**
 * @author feige
 * @date 2023-10-10
 * @version 1.0
 * @desc  保存用户购买简历模板信息
 */
func (service *ResumeTemplateCodeMapper) SaveUserBuyResumeTemplate(userBuyResumeTemplate model.UserBuyResumeTemplate) bool {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		// 保存用户简历模板订单
		_, err = txOrm.Insert(&userBuyResumeTemplate)
		return err
	})
	return err == nil
}
